import time

from elasticsearch import Elasticsearch
from pandasticsearch import DataFrame
import pandas as pd

if __name__ == '__main__':
    # df = DataFrame.from_es(url='http://10.2.111.56:9200', index='ehlindex20180724', doc_type='pass_car')
    df = DataFrame.from_es(url='http://10.2.111.51:9200', index='ehlindex201809_10', doc_type='pass_car'
                           ).limit(10)
    dfs = DataFrame.from_es(url='http://10.2.111.56:9200', index='ehlindex201809_10', doc_type='pass_car'
                           ).limit(10)
    # sf = df.groupby('bay_id').collect()
    # sk = df.filter((df['timestamp']>1539694989572 )&(df['timestamp']<1539694999572)).to_pandas()
    df.filter((df['timestamp'] > 1539694989572) & (df['timestamp'] < 1539694999572)).groupby('bay_id').select(
        'bay_id').to_pandas()
    ma = df.agg(df['gcsj'].max).to_pandas().iloc[0][0]
    nowint = int(pd.Timestamp.now().value / 10 ** 6)
    count = df.filter(df['gcsj'] > nowint).count().to_pandas().iloc[0][0]

    count = df.filter(df['gcsj'] > 1326039192000).count().to_pandas().iloc[0][0]
    data = df.filter(df.gcsj > 1526039192000).select('gcsj','nwrksj').to_pandas()[['gcsj','nwrksj']]
    data.loc[data.nwrksj<data.gcsj,'nwrksj'] = data['gcsj']
    delay = (data.nwrksj - data.gcsj)/1000

    es = Elasticsearch('10.2.111.56', http_auth=('admin', 'admin'),port=9200)
    query = {
        "query": {
            "bool": {
                "must": [{"range":
                    {"gcsj":
                        {
                            "gte": 1525987510000
                        }
                    }}]
            }
        }
    }
    # query = {
    #                 "query": {
    #                     "bool": {
    #                         "must": [{"term":
    #                                     {
    #                                         "kkbh":"131000100236"
    #
    #                                     }
    #                                   },
    #                             {"term":
    #                                 {
    #                                     "hpzl":"01"
    #                                 }
    #                             }
    #                         ]
    #                     }
    #                 }
    #             }


    s = es.count(index="ehlindex201805", doc_type="pass_car",body=query)

    print('ok')
